<template>
  <div class="app-container">
    <div class="filterDiv">
      <el-input v-model="listQuery.brandName" size="small" placeholder="品牌代码/名称" style="width: 200px;" class="filter-item" />
      <el-button size="small" type="primary" round @click="getList">查询</el-button>
      <el-button v-permiss="['60205070']" style="float:right" size="small" type="primary" round @click="showImport">导入</el-button>
      <el-button v-permiss="['60205020']" size="small" type="primary" round style="float:right" @click="handleAdd">新增</el-button>
    </div>
    <div class="contentDiv">
      <el-table :key="tableKey" v-loading="listLoading" :data="tableData" fit highlight-current-row style="width: 100%;" size="small">
        <el-empty slot="empty" :image-size="120" description="暂无品牌" />
        <el-table-column label="序号" type="index" width="100" align="center" />
        <el-table-column label="品牌代码" align="center">
          <template slot-scope="{row}">
            <span>{{ row.brandCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="品牌名称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.brandName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center">
          <template slot-scope="{row}">
            <span>{{ row.remarks }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permiss="['60205050']" label="是否启用" align="center" width="120">
          <template slot-scope="{row}">
            <el-switch v-model="row.isDisable" :active-value="0" :inactive-value="1" @change="updateStatus($event, row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="{row}">
            <el-button v-permiss="['60205030']" type="text" size="small" @click="handleCompile(row)">编辑</el-button>
            <el-button v-permiss="['60205060']" type="text" size="small" @click="handleDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :close-on-click-modal="false" :title="dialogStatus=='create'?'新增品牌':'修改品牌'" :visible.sync="dialogVisible" width="460px">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="90px" style="width: 400px; margin-left:10px;">
        <el-form-item label="品牌代码" prop="brandCode">
          <el-input v-model="form.brandCode" placeholder="品牌代码" />
        </el-form-item>
        <el-form-item label="品牌名称" prop="brandName">
          <el-input v-model="form.brandName" placeholder="品牌名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="备注" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isDisable">
          <el-radio v-model="form.isDisable" :label="0">是</el-radio>
          <el-radio v-model="form.isDisable" :label="1">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus == 'create'?handleCreate():handleModify()">确定</el-button>
      </div>
    </el-dialog>
    <import-data ref="importRef" type="brand" title="品牌" />
  </div>
</template>

<script>
import { getBrand, saveBrand, delBrand, updateBrandDisabled } from '@/api/basedata'
import importData from './importData'
export default {
  name: 'BaseBrand',
  components: { importData },
  data() {
    return {
      tableKey: 0,
      tableData: [],
      total: 0,
      listLoading: true,
      listQuery: {
        brandName: ''
      },
      form: {
        brandName: '',
        brandCode: '',
        remarks: '',
        isDisable: 0
      },
      dialogVisible: false,
      dialogStatus: '',
      rules: {
        brandName: [{ required: true, message: '名称不能为空', trigger: 'change' }],
        brandCode: [{ required: true, message: '代码不能为空', trigger: 'change' }],
        isDisable: [{ required: false }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    showImport() {
      this.$refs.importRef.option = {
        show: true
      }
    },
    getList() {
      this.listLoading = true
      getBrand(this.listQuery).then(response => {
        this.listLoading = false
        this.tableData = response.data.data
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleAdd(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'create'
      this.form.id = ''
      this.form.brandName = ''
      this.form.brandCode = ''
      this.form.parentCode = ''
      this.form.isDisable = 0
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCompile(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'update'
      this.form.id = obj.id
      this.form.brandName = obj.brandName
      this.form.brandCode = obj.brandCode
      this.form.parentCode = obj.parentCode
      this.form.isDisable = obj.isDisable
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateStatus(e, row) {
      this.changeAvaiable(e, row.id)
    },
    changeAvaiable(isDisable, id) {
      var obj = { isDisable, id }
      updateBrandDisabled(obj).then(res => {
        if (res.data.errorCode === '0') {
          this.$message.success(res.data.msg)
        }
      })
    },
    handleModify() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveBrand(this.form).then(res => {
            if (res.data.errorCode === '0') {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    handleCreate() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveBrand(this.form).then(res => {
            if (res.data.errorCode === '0') {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    handleDel(id) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delBrand(id).then(res => {
          if (res.data.errorCode === '0') {
            this.getList()
            this.dialogVisible = false
            this.$message.success(res.data.msg)
          }
        })
      })
    }
  }
}
</script>
<style scoped>
.filter-item {
  position: relative;
}
/deep/.el-form-item {
  margin-bottom: 18px;
}
</style>
